package com.cet.qz.unit01.chapter1_1;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description:有序数组去重
 * @author: 陈恩涛
 * @create: 2020-12-17 21:21
 **/
public class Qz1_1_28 {

    /*
     *方法描述： 统计数组重复元素个数，用于确定无重复数组长度
     *
     * @param a
     * @return int
     * @author chenentao
     * @date 2020/12/17 21:42
     */
    public static int countRepItem(int[] a){
        int repNums = 0;
        for (int i = 1; i < a.length; i++) {
            if(a[i] == a[i-1])
                repNums++;
        }
        return repNums;
    }

    /*
     *方法描述： 对有序数组去重
     *
     * @param a
     * @param repNums
     * @return int[]
     * @author chenentao
     * @date 2020/12/17 21:42
     */
    public static int[] getNoRepArray(int[] a,int repNums){

        int[] noRepArray = new int[a.length - repNums];
        int i = 0;

        //妙笔生花
        noRepArray[i++] = a[0];

        for (int j = 1; j < a.length; j++) {
            if(a[j - 1] != a[j]){
                noRepArray[i] = a[j];
                i++;
            }
        }
        return noRepArray;
    }

    public static void main(String[] args) {
        int[] a = {1,2,3,4,5,6,7,6,7,9,5,3,3,3,4,5,5,5};
        Arrays.sort(a);
        //System.out.println(countRepItem(a));
        for (int i : getNoRepArray(a,countRepItem(a))) {
            System.out.println(i);
        }
    }

}
